package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsDevBindInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsDevBindInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.DevBindInfo;
import cc.rengu.igas.bsps.common.entity.MchntBindInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.realize.PublicDealService;
import cc.rengu.igas.bsps.core.realize.impl.PublicDealServiceImpl;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.bean.BindDevInfoBean;
import cc.rengu.igas.bsps.facade.request.VoiceDevBindListQueryRequset;
import cc.rengu.igas.bsps.facade.response.VoiceDevBindListQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import org.springframework.util.CollectionUtils;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020/04/03
 */
public class VoiceDevBindListQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        VoiceDevBindListQueryRequset voiceDevBindListQueryRequset = new VoiceDevBindListQueryRequset();
        ConvertUtil.convertOutput(voiceDevBindListQueryRequset);
        return voiceDevBindListQueryRequset;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        VoiceDevBindListQueryRequset voiceDevBindListQueryRequset = (VoiceDevBindListQueryRequset) request;
        VoiceDevBindListQueryResponse voiceDevBindListQueryResponse = new VoiceDevBindListQueryResponse();

        /*获取请求报文信息:法人行号,用户标识,商户号,绑定设备类型*/
        String instId = voiceDevBindListQueryRequset.getHeader().getInstId();
        String userId = voiceDevBindListQueryRequset.getUserId();
        String mchntNo = voiceDevBindListQueryRequset.getMchntNo();
        String bindDevType = voiceDevBindListQueryRequset.getBindDevType();
        /*必填校验*/
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(mchntNo)) {
            rglog.error("必填性校验失败!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*查询收银员信息*/
        PublicDealService publicDealService = new PublicDealServiceImpl();
        List<MchntBindInfo> mchntBindInfoList = publicDealService.mchntCashierListQuery(instId, mchntNo, userId);
        List<BindDevInfoBean> bindDevInfoList = new ArrayList<>();
        /*根据收银员信息及绑定设备类型查询收银员绑定设备信息*/
        if (!CollectionUtils.isEmpty(mchntBindInfoList)) {
            mchntBindInfoList.forEach(item -> convBindDevInfo(item, bindDevType, bindDevInfoList));
        }

        /* 组装响应报文 */
        BizResponse<VoiceDevBindListQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        voiceDevBindListQueryResponse.setBindDevInfoList(bindDevInfoList);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(voiceDevBindListQueryResponse);
        return bizResponse;
    }

    /**
     * 根据收银员信息查询绑定设备信息列表
     * cashierInfo 收银员信息
     * bindDevType 设备类型
     * bindDevInfoList 收银员绑定设备信息
     */
    private void convBindDevInfo(MchntBindInfo mchntBindInfo, String bindDevType, List<BindDevInfoBean> bindDevInfoList) {
        try {
            BspsDevBindInfoMapper bspsDevBindInfoMapper = new BspsDevBindInfoMapperImpl();
            /*根据法人行，收银员ID，绑定类型，商户号*/
            //收银员id,cashierInfo属性
            String cahsierUserId = mchntBindInfo.getBindUserId();
            //收银员绑定商户号,cashierInfo属性
            String cashierMchntNo = mchntBindInfo.getMchntNo();
            List<DevBindInfo> devBindInfoList = bspsDevBindInfoMapper.selectBspsDevBindInfoListByUserIdAndDevType(mchntBindInfo.getInstId(), cahsierUserId, cashierMchntNo, bindDevType);
            if (!CollectionUtils.isEmpty(devBindInfoList)) {
                for (DevBindInfo devBindInfo : devBindInfoList) {
                    BindDevInfoBean bindDevInfoBean = new BindDevInfoBean();
                    bindDevInfoBean.setBindDevType(devBindInfo.getBindDevType());
                    /*收银员绑定设备信息拼装*/
                    bindDevInfoBean.setBindUserId(devBindInfo.getBindUserId());
                    //取调用收银员列表查询后返回的收银员对象信息
                    bindDevInfoBean.setBindUserName(mchntBindInfo.getBindNickName());
                    //取调用收银员列表查询后返回的收银员对象信息
                    bindDevInfoBean.setBindUserRole(mchntBindInfo.getBindUserRole());
                    bindDevInfoBean.setBindDevId(devBindInfo.getBindDevId());
                    bindDevInfoBean.setBindDevModel(devBindInfo.getBindDevModel());
                    bindDevInfoBean.setBindProductId(devBindInfo.getBindDevBrand());
                    bindDevInfoBean.setBindStatus(devBindInfo.getBindStatus());
                    bindDevInfoBean.setMsgPushFlag(devBindInfo.getMsgPushFlag());
                    bindDevInfoBean.setMsgPushKey(devBindInfo.getMsgPushKey());
                    bindDevInfoBean.setBindDevBrand(devBindInfo.getBindDevBrand());
                    bindDevInfoList.add(bindDevInfoBean);
                }
            }
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("收银员语音设备绑定信息列表查询详细信息转换失败:{}", byteArrayOutputStream.toString());
        }
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        VoiceDevBindListQueryResponse voiceDevBindListQueryResponse = (VoiceDevBindListQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(voiceDevBindListQueryResponse);
    }
}
